import ctypes
import sys
import pyautogui
import time
import win32gui, win32ui, win32con, win32api, win32com

if __name__ == '__main__':
    # 获取管理员
    if not ctypes.windll.shell32.IsUserAnAdmin():
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
    # 获取圣遗物个数
    number = int(input("你有多少个圣遗物"))
    import get_relic_by_image
    # 将原神置于前方
    handle = win32gui.FindWindow(0, "原神")
    win32gui.SendMessage(handle, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0)  # 这句很重要，这句可以保证将句柄置到前端
    win32gui.SetForegroundWindow(handle)
    # 获取窗口位置
    x, y, x1, y1 = win32gui.GetWindowRect(handle)
    x += 3
    y += 32
    # 置于前方后要等一会，因为有动画
    time.sleep(0.2)
    # 计数器，识别一次就加一，一旦识别次数大于圣遗物个数就不再识别了
    pointer = 0
    for a in range(38):
        if pointer > number-1:
            break
        for i in range(5):
            if pointer > number-1:
                break
            for z in range(8):
                if pointer > number-1:
                    break
                # 移动鼠标并单击
                win32api.SetCursorPos((x + 150 + 122 * z, y + 140 + i * 146))
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN | win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0)
                time.sleep(0.01)
                img = pyautogui.screenshot(region=[x + 1090, y + 100, 410, 460])
                print(get_relic_by_image.get_relic_by_image(img).tostring1())
                print("识别" + str(pointer) + "次")
                pointer += 1

        # 翻页，并且每个三次往回翻一点，因为每个三次就会错开一点
        for i in range(49):
            win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -1)
        time.sleep(0.5)
        if (a + 1) % 3 == 0:
            win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, 1)

    # img = pyautogui.screenshot(region=[x + 3, y + 32, x1 - x - 6, y1 - y - 35])  # x,y,w,h
    img = pyautogui.screenshot(region=[x + 3 + 1090, y + 32 + 100, 410,460])  # x,y,w,h
    # img.show()
    print(get_relic_by_image.get_relic_by_image(img).tostring1())
    time.sleep(100)